home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / gems / g_gemsi.lha / GraphicsGems / PolyScan / scantest.c < prev   
Encoding:
C/C++ Source or Header  |  1992-04-10  |  1.8 KB  |  74 lines

  1.  
  2.  
  3. /*
  4.  * scantest.c: use poly_scan() for Gouraud shading and z-buffer demo.
  5.  * Given the screen space X, Y, and Z of N-gon on command line,
  6.  * print out all pixels during scan conversion.
  7.  * This code could easily be modified to actually read and write pixels.
  8.  *
  9.  * Paul Heckbert    Dec 1989
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include "poly.h"
  15.  
  16. #define XMAX 1280    /* hypothetical image width */
  17. #define YMAX 1024    /* hypothetical image height */
  18.  
  19. #define FRANDOM() ((rand()&32767)/32767.)   
  20.         /* random number between 0 and 1 */
  21.  
  22. static void pixelproc();
  23.  
  24.  
  25. main(ac, av)
  26. int ac;
  27. char **av;
  28. {
  29.     int i;
  30.     Poly p;
  31.     static Window win = {0, 0, XMAX-1, YMAX-1};
  32.         /* screen clipping window */
  33.  
  34.     if (ac<2 || ac != 2+3*(p.n = atoi(av[1]))) {
  35.         fprintf(stderr, 
  36.             "Usage: scantest N X1 Y1 Z1 X2 Y2 Z2 ... XN YN ZN\n");
  37.         exit(1);
  38.     }
  39.     for (i=0; i<p.n; i++) {
  40.         p.vert[i].sx = atof(av[2+3*i]);    /* set screen space x,y,z */
  41.         p.vert[i].sy = atof(av[3+3*i]);
  42.         p.vert[i].sz = atof(av[4+3*i]);
  43.         p.vert[i].r = FRANDOM();  /* random vertex colors, for kicks */
  44.         p.vert[i].g = FRANDOM();
  45.         p.vert[i].b = FRANDOM();
  46.     }
  47.     /* interpolate sx, sy, sz, r, g, and b in poly_scan */
  48.     p.mask = POLY_MASK(sx) | POLY_MASK(sy) | POLY_MASK(sz) |
  49.         POLY_MASK(r) | POLY_MASK(g) | POLY_MASK(b);
  50.  
  51.     poly_print("scan converting the polygon", &p);
  52.     
  53.     poly_scan(&p, &win, pixelproc);    /* scan convert! */
  54. }
  55.  
  56. static void pixelproc(x, y, point)
  57.         /* called at each pixel by poly_scan */
  58. int x, y;
  59. Poly_vert *point;
  60. {
  61.     printf("pixel (%d,%d) screenz=%g rgb=(%g,%g,%g)\n",
  62.         x, y, point->sz, point->r, point->g, point->b);
  63.  
  64.     /*
  65.      * in real graphics program you could read and write pixels, e.g.:
  66.      *
  67.      *    if (point->sz < zbuffer_read(x, y)) {
  68.      *        image_write_rgb(x, y, point->r, point->g, point->b);
  69.      *        zbuffer_write(x, y, point->sz);
  70.      *  }
  71.      */
  72. }
  73.  
  74.